<template>
  <div class="course">
    <!-- 新增课程按钮 -->
    <el-button size="small" type="warning" class="course__add" @click="showAddCourseFn">新增课程</el-button>
    
    <!-- 课程表格 -->
    <el-table :data="courses" border row-key="_id" :expand-row-keys="expandKeys" class="table">
     <!-- 折叠学生面板 -->
      <el-table-column type="expand">
        <template scope="scope">
          <div class="table__students">
            <!-- 学生信息 -->
            <div class="table__students-user" v-for="item in scope.row.students" :key="item._id">
              <el-tooltip :content="`学号: ${item.code ? item.code : '暂未填写'}`">
                <img :src="item.avatar" alt="" :class="{'table__students-user-img': true, 'active':!item.name }">
              </el-tooltip>
              <span class="table__students-user-name" v-if="item.name">{{item.name}}</span>
              <span class="table__students-user-name" style="color:#cccccb;" v-else>{{item.wx_name}}</span>
            </div>
          </div>
        </template>
      </el-table-column>
      <!-- 课程代码 -->
      <el-table-column prop="code" label="课程代码" width="200"></el-table-column>
      <!-- 课程名字 -->
      <el-table-column prop="name" label="课程名字" width="200"></el-table-column>
      <!-- 创建日期 -->
      <el-table-column prop="create_time" label="创建日期" width="250">
        <template scope="scope">
          <span>{{new Date(scope.row.create_time).toLocaleString()}}</span>
        </template>
      </el-table-column>      
      <!-- 操作 -->
      <el-table-column label="操作">
        <template scope="scope">
          <el-button size="mini" type="primary" @click="showCodeDialog(scope.row._id, scope.row.name)">看二维码</el-button>
          <el-button size="mini" type="success" @click="refreshStudentFn(scope.row._id)">刷新学生</el-button>
          <el-button size="mini" type="info" @click="showAddSignFn(scope.row._id)">新增签到</el-button>
          <el-button size="mini" type="danger" @click="ToSignFn(scope.row._id)">查看签到</el-button>
        </template>
      </el-table-column>
    </el-table>
   
    <!-- 分页 -->
    <el-pagination :total="total" :page-size="pageSize" class="course__page" @current-change="changePageFn"></el-pagination>
   
    <!-- 新增课程 -->
    <el-dialog title="新增课程" :visible.sync="isAddCourse" size="tiny" @close="closeAddCourseFn">
      <!-- 表单 -->
      <el-form :inline="true" :model="addCourseForm" :rules="addCourseRules" ref="addCourseForm">
        <el-form-item prop="code" label="课程代码">
          <el-input placeholder="例如 TT0039" v-model="addCourseForm.code"></el-input>
        </el-form-item>
        <el-form-item prop="name" label="课程名字">
          <el-input placeholder="例如 个人理财案例分析" v-model="addCourseForm.name"></el-input>
        </el-form-item>
      </el-form>
      <!-- 尾部 -->
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addCourseFn">保存</el-button>
      </span>
    </el-dialog>

    <!-- 新增签到 -->
    <el-dialog title="新增签到" :visible.sync="addSign.isShowDialog" size="tiny" @close="closeAddSignFn">
      <!-- 表单 -->
      <el-form :inline="true" :model="addSign.form" :rules="addSign.rules" ref="addSignForm">
        <!-- 开始时间 -->
        <el-form-item prop="begin_time" label="开始时间">
          <el-date-picker v-model="addSign.form.begin_time" type="datetime" placeholder="开始时间"></el-date-picker>
        </el-form-item>
        <!-- 结束时间 -->
        <el-form-item prop="end_time" label="结束时间">
          <el-date-picker v-model="addSign.form.end_time" type="datetime" placeholder="结束时间"></el-date-picker>
        </el-form-item>
        <!-- 地点 -->
        <el-form-item prop="site" label="签到地点">
          <el-input placeholder="例如 T401" v-model="addSign.form.site"></el-input>
        </el-form-item>
      </el-form>
      <!-- 尾部 -->
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addSignFn">保存</el-button>
      </span>
    </el-dialog>

    <!-- 查看二维码 -->
    <el-dialog :title="showCode.title" :visible.sync="showCode.isShow" size="tiny" @close="closeCodeDialog">
      <img :src="showCode.codeUrl" alt="" style="width:100%;height:100%;">
    </el-dialog>
  
  </div>
</template>

<script>
import {pageSize} from '@/config'

export default {
  name: 'course',
  created () {
    this.applyData(this.pageNum)
  },
  data () {
    return {
      // 课程数据
      courses: [],
      // 总课程数
      total: 0,
      // 每页显示的条数
      pageSize: pageSize,
      // 当前页码
      pageNum: 1,

      // 默认展开的行
      expandKeys: [],

      // 是否显示新增课程弹窗
      isAddCourse: false,
      // 新增课程的表单
      addCourseForm: {code: '', name: ''},
      // 新增课程的表单规则
      addCourseRules: {
        code: [
          { required: true, message: '请输入课程代码', trigger: 'blur' },
          { min: 6, max: 6, message: '长度在6个字符', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入课程名字', trigger: 'blur' }
        ]
      },

      // 新增签到
      addSign: {
        isShowDialog: false,
        form: {begin_time: '', end_time: '', site: ''},
        rules: {
          begin_time: [
            { type: 'date', required: true, message: '请选择时间', trigger: 'blur' }
          ],
          end_time: [
            { type: 'date', required: true, message: '请选择时间', trigger: 'blur' }
          ],
          site: [
            { required: true, message: '请输入填写地点', trigger: 'blur' }
          ]
        },
        course_id: ''
      },

      // 二维码弹窗
      showCode: {
        isShow: false,
        title: '',
        codeUrl: ''
      }
    }
  },
  methods: {
    // 请求应用数据
    applyData (pageNum) {
      this.$store.dispatch('getCourseByPage', { pageNum }).then(res => {
        this.courses = res.data.lists
        this.total = res.data.total
        // 默认打开行
        this.expandKeys = [].concat(this.expandKeys)
      })
    },
    // 改变页码
    changePageFn (page) {
      this.pageNum = page
      this.applyData(page)
    },
    // 显示新增课程弹窗
    showAddCourseFn () {
      this.isAddCourse = true
    },
    // 关闭新增课程弹窗
    closeAddCourseFn () {
      this.$refs.addCourseForm.resetFields()
    },
    // 新增课程
    addCourseFn () {
      this.$refs.addCourseForm.validate(valid => {
        if (valid) {
          this.$store.dispatch('addCourse', {
            code: this.addCourseForm.code,
            name: this.addCourseForm.name
          }).then(res => {
            this.$message('新增成功')
            this.isAddCourse = false
            this.$refs.addCourseForm.resetFields()
            this.applyData(this.pageNum)
          })
        } else {
          return false
        }
      })
    },
    // 显示二维码弹窗
    showCodeDialog (cid, name) {
      this.showCode.title = name
      this.showCode.codeUrl = `/others/make_code?url=/course/addStudent?cid=${cid}`
      this.showCode.isShow = true
    },
    // 关闭二维码弹窗
    closeCodeDialog () {
      this.showCode.codeUrl = ''
    },
    // 查看二维码
    ToSignFn (cid) {
      this.$router.push('/sign?cid=' + cid)
    },
    // 刷新学生
    refreshStudentFn (cid) {
      this.expandKeys = [cid]
      this.applyData(this.pageNum)
    },
    // 显示新增签到弹窗
    showAddSignFn (cid) {
      this.addSign.isShowDialog = true
      this.addSign.course_id = cid
    },
    // 关闭新增签到弹窗
    closeAddSignFn () {
      this.$refs.addSignForm.resetFields()
    },
    // 新增一个签到
    addSignFn () {
      this.$refs.addSignForm.validate(valid => {
        if (valid) {
          let {begin_time, end_time, site} = this.addSign.form
          this.$store.dispatch('addSign', {begin_time, end_time, site, course_id: this.addSign.course_id}).then(res => {
            this.$message('新增签到成功')
            this.$router.push('/sign?cid=' + this.addSign.course_id)
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped>
  .course {
    overflow: scroll;
  }
  /* 添加课程按钮 */
  .course__add {
    margin-bottom: 10px;
  }
  /* 分页 */
  .course__page {
    margin-top: 10px;
  }
  /* 课程学生 */
  .table__students {
    display: flex;
    flex-wrap: wrap;
  }
  /* 学生信息 */
  .table__students-user {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 50px;
    margin-top: 10px;
    margin-right: 10px;
  }
  .table__students-user-img {
    width: 30px;
    height: 30px;
    border-radius: 30px;
  }
  .table__students-user-img.active {
    -webkit-filter:grayscale(1);
	  filter:grayscale(1);
  }
  .table__students-user-name {
    font-size: 12px;
    color: #666;
  }
</style>
